#!/usr/bin/env python
# coding: utf-8

###############################################################
#   工具类
###############################################################
import random
from datetime import *
import time
import uuid
import xml.etree.ElementTree as elementTree
import platform
import os


from binascii import a2b_hex

import time
import hmac
import hashlib
import base64
import urllib


class Utils(object):

    @staticmethod
    def ding_sign():
        secret = 'SECd867a1053466d09362870db83e885d4965e7bafb1dc30190a4ef559337c1c94b'
        timestamp = str(round(time.time() * 1000))
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
        return '&timestamp={}&sign={}'.format(timestamp, sign)

    @staticmethod
    def getSign():

        return "d38dWjdjjdj#97uu!hhfju638dhjfkap83lvncu!d8jdjf8!jd8888889999"

    @staticmethod
    def getUploadSig():
        return "93jjfued810d9dDDDEIN2D00DKMDKj3dkf778ddjdhd772hdndhdydyedhdhQ"

    def encodeurl(self, cs):
        from urllib.parse import quote
        cs = quote(cs, 'utf-8')
        return cs

    def decodeurl(self, cs):
        from urllib.parse import unquote
        cs = unquote(cs, 'utf-8')
        return cs

    @staticmethod
    def getSeparator():
        if 'Windows' in platform.system():
            separator = '\\'
        else:
            separator = '/'
        return separator

    @staticmethod
    def int2str_time(int_time):
        time_struct = time.localtime(int_time)  # 秒数
        time_format = time.strftime('%Y-%m-%d', time_struct)
        return time_format

    @staticmethod
    def findPath(file):
        o_path = os.getcwd()
        separator = Utils.getSeparator()
        str = o_path
        str = str.split(separator)
        while len(str) > 0:
            spath = separator.join(str) + separator + file
            leng = len(str)
            if os.path.exists(spath):
                return spath
            str.remove(str[leng - 1])

    @staticmethod
    def load_js():
        file_path = Utils.findPath('md5.js')
        print(file_path)
        f = open(file_path, 'r', encoding='utf-8')  # 打开JS文件
        line = f.readline()
        js_str = ''
        while line:
            js_str = js_str + line
            line = f.readline()
        return js_str



    def __unpad( text):
            pad = ord(text[-1])
            return text[:-pad]


    def md5(payload):
        """ md5加密算法 """
        import hashlib
        m = hashlib.md5()
        b = payload.encode(encoding='utf-8')
        m.update(b)
        return m.hexdigest()


